<?php
namespace App\Components\Admin;

use Illuminate\Database\Eloquent\Builder;

class Paginator
{
    public static function paginate(Builder $eloquent, array $params = [], string $url = ''): array
    {
        $return = [
            'count' => $eloquent->count(),
        ];
        if (!isset($params['pageSize']) || $params['pageSize'] <= 0) {
            $params['pageSize'] = 20;
        }
        if (!isset($params['page']) || $params['page'] <= 0) {
            $params['page'] = 1;
        }
        $return['link'] = self::generatePagingHtml($return['count'], $params['page'], $params, $url);
        $return['data'] = $eloquent->offset(($params['page'] - 1) * $params['pageSize'])
            ->limit($params['pageSize'])->get()->toArray();
        return $return;
    }

    private static function generatePagingHtml(int $count, int $pageNum, array $params, string $url): string
    {
        $pageCount = ceil($count / $params['pageSize']);
        if ($pageNum > $pageCount) {
            $pageNum = $pageCount;
        }
        $htmlStr = '';
        if ($pageCount > 1) {
            if (isset($params['page'])) {
                unset($params['page']);
            }
            if (strpos($url, '?') !== false) {
                $url .= '&' . http_build_query($params);
            } else {
                $url .= '?' . http_build_query($params);
            }
            $url = rtrim($url, '&');
            $url .= '&page=';

            $htmlStr .= '<ul class="pagination">';
            if ($pageNum > 1) {
                $htmlStr .= '<li class="page-item"><a class="page-link" href="' . $url . ($pageNum - 1) . '">Previous</a></li>';
            } else {
                $htmlStr .= '<li class="page-item disabled"><a class="page-link" href="#">Previous</a></li>';
            }

            if (($pageNum - 6) >= 0) {
                $htmlStr .= '<li class="page-item"><a class="page-link" href="' . $url . '1">1 ...</a></li>';
            }

            if (($pageNum - 3) >= 1) {
                $htmlStr .= '<li class="page-item"><a class="page-link" href="' . $url . ($pageNum - 3) . '">' . ($pageNum - 3) . '</a></li>';
            }
            if (($pageNum - 2) >= 1) {
                $htmlStr .= '<li class="page-item"><a class="page-link" href="' . $url . ($pageNum - 2) . '">' . ($pageNum - 2) . '</a></li>';
            }
            if (($pageNum - 1) >= 1) {
                $htmlStr .= '<li class="page-item"><a class="page-link" href="' . $url . ($pageNum - 1) . '">' . ($pageNum - 1) . '</a></li>';
            }

            $htmlStr .= '<li class="page-item active"><a class="page-link" href="#">' . $pageNum . '</a></li>';

            if (($pageNum + 1) <= $pageCount) {
                $htmlStr .= '<li class="page-item"><a class="page-link" href="' . $url . ($pageNum + 1) . '">' . ($pageNum + 1) . '</a></li>';
            }
            if (($pageNum + 2) <= $pageCount) {
                $htmlStr .= '<li class="page-item"><a class="page-link" href="' . $url . ($pageNum + 2) . '">' . ($pageNum + 2) . '</a></li>';
            }
            if (($pageNum + 3) <= $pageCount) {
                $htmlStr .= '<li class="page-item"><a class="page-link" href="' . $url . ($pageNum + 3) . '">' . ($pageNum + 3) . '</a></li>';
            }
            if (($pageNum + 4) <= $pageCount) {
                $htmlStr .= '<li class="page-item"><a class="page-link" href="' . $url . ($pageNum + 4) . '">' . ($pageNum + 4) . '</a></li>';
            }
            if (($pageNum + 5) <= $pageCount) {
                $htmlStr .= '<li class="page-item"><a class="page-link" href="' . $url . ($pageNum + 5) . '">' . ($pageNum + 5) . '</a></li>';
            }
            if (($pageNum + 6) <= $pageCount) {
                $htmlStr .= '<li class="page-item"><a class="page-link" href="' . $url . $pageCount . '">... ' . $pageCount . '</a></li>';
            }

            if (($pageNum + 1) <= $pageCount) {
                $htmlStr .= '<li class="page-item"><a class="page-link" href="' . $url . ($pageNum + 1) . '">Next</a></li>';
            } else {
                $htmlStr .= '<li class="page-item disabled"><a class="page-link" href="#">Next</a></li>';
            }
        }
        return $htmlStr;
    }
}
